package com.suda.catalina.myapplication;

import android.content.Context;
import android.text.TextUtils;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;

/**
 * Created by gzy on 2016/3/15.
 */
public class EditFile {

    public static void saveData(Context context, String str) {

        BufferedWriter bufferedWriter = null;
        try {
            FileOutputStream fileOutputStream = context.openFileOutput("data", Context.MODE_PRIVATE);
            bufferedWriter = new BufferedWriter(new OutputStreamWriter(fileOutputStream));
            bufferedWriter.write(str);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (bufferedWriter != null) {
                try {
                    bufferedWriter.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    public static String loadData (Context context) {

        BufferedReader bufferedReader = null;
        StringBuilder data = new StringBuilder();
        try {
            FileInputStream fileInputStream = context.openFileInput("data");
            bufferedReader = new BufferedReader(new InputStreamReader(fileInputStream));
            String str = "";
            while (!TextUtils.isEmpty(str = bufferedReader.readLine())) {
                data.append(str);
            }
        } catch (java.io.IOException e) {
            e.printStackTrace();
        } finally {
            if (bufferedReader != null) {
                try {
                    bufferedReader.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return data.toString();
    }
}
